import { mAxios } from "./axios.js";
import { useTreeStore } from "~/utils/pinia/tree.js";
import { strategyFn } from "../../assets/js/strategyType.js";
import { nextTick } from "vue";
import Cookies from 'js-cookie';

export function HETAxios (type, url, data = {}) {
  // if (!treeStore.treeDeviceId && url.indexOf('/null/') != -1) return false
  return new Promise(function (resolve, reject) {
    let promise
    if (type === 'GET') {
      url += '?' + urlCombine(data)
      promise = mAxios.get(url)
    } else if(type === 'POST') {
      promise = mAxios.post(url, data)
    } else if(type === 'PUT') {
      promise = mAxios.put(url, data)
    } else if(type === 'DELETE') {
      promise = mAxios.delete(url, { data })
    }
    promise.then(req => {
      resolve(req)
      nextTick(() => {
        let strategyType = strategyFn()
        const treeStore = useTreeStore();
        treeStore.treeStrategyType = strategyType || null
      })
    }).catch(error => {
      reject(error)
    })
  })
}
export const urlCombine = (data) => {
  let dataStr = ''
  Object.keys(data).forEach(key => {
    dataStr += key + '=' + data[key] + '&'
  })
  if (dataStr !== '') {
    dataStr = dataStr.substring(0, dataStr.lastIndexOf('&'))
  }
  return dataStr
}